<template>
  <div id="table_container">
    <el-card style="margin-bottom: 5px; text-align: left">
      <el-input v-model="keyWord" placeholder="输入名称、编码" style="width: 200px;" @keyup.enter.native="handleFilter"></el-input>
      <el-select v-model="level" placeholder="选择级别" style="margin-left: 10px; width: 200px;">
        <el-option v-for="item in importanceOptions" :key="item.value" :label="item.label" :value="item.value"/>
      </el-select>
      <el-button type="primary" icon="el-icon-search" @click="handleFilter()" style="margin-left: 10px;">
        查询
      </el-button>

    </el-card>
    <el-card>
      <el-table
          :data="tableData"
          v-loading="loading"
          row-key="id"
          border
          lazy
          :load="load"
          :tree-props="{children: 'children', hasChildren: 'hasChildren'}">
        <el-table-column
            type="index"
            label="序号"
            align="center"
            width="50">
        </el-table-column>
        <el-table-column
            prop="name"
            label="名称"
            width="280">
        </el-table-column>
        <el-table-column
            prop="id"
            label="编码">
        </el-table-column>
        <el-table-column
            prop="level"
            label="层级" width="215">
          <template slot-scope="scope">
            <el-tag
                :type="scope.row.level === 1 ? 'danger'
              :scope.row.level === 2 ? 'warning'
              :scope.row.level === 3 ? 'primary'
              :scope.row.level === 4 ? 'success':'info'"
                disable-transitions>{{ scope.row.level_label }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column
            prop="pinyin"
            label="拼音">
        </el-table-column>
        <el-table-column
            prop="initial"
            label="首字母"
            align="center"
            width="75">
        </el-table-column>
        <el-table-column
            prop="status"
            label="状态"
            align="center"
            width="70">
          <template slot-scope="scope">
            <el-tag
                :type="scope.row.status === true ? 'success' :'danger' "
                disable-transitions>{{ scope.row.status_label }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column
            label="操作">
        </el-table-column>
      </el-table>
    </el-card>
  </div>
</template>

<script>
import {getArea} from "@/api";

export default {
  name: "index",
  data() {
    return {
      loading: true,
      tableData: [],

      importanceOptions: [
        {label: '省级（省份、直辖市、自治区）', value: 1},
        {label: '地级（城市）', value: 2},
        {label: '县级（区县）', value: 3},
        {label: '乡级（乡镇、街道）', value: 4},
        {label: '村级（村委会、居委会）', value: 5},
      ],
      level: undefined,
      keyWord: undefined,
    }
  },
  mounted() {
    this.getDataList()
  },
  methods: {
    load(tree, treeNode, resolve) {
      getArea({parentId: tree.id}).then(res => {
        resolve(res.data)
      }).catch(error => {
        console.log('获取下级地区子数据失败！', error)
      })
    },
    getDataList(params) {
      getArea(params).then(res => {
        this.tableData = res.data
        setTimeout(() => {
          this.loading = false
        }, 1.5 * 500);
      })
    },
    handleFilter() {
      let params = {
        keyWord: this.keyWord,
        level: this.level
      };
      this.getDataList(params)
    }
  },
}
</script>

<style scoped>

</style>